package com.hfut.jpoa.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import com.hfut.jpoa.common.dto.ResultDto;
import com.hfut.jpoa.common.exception.DaoException;
import com.hfut.jpoa.dao.UserDAO;
import com.hfut.jpoa.dto.UserDTO;
import com.hfut.jpoa.model.User;
import com.ibatis.sqlmap.client.SqlMapClient;

public class UserDAOImpl implements UserDAO
{
	private SqlMapClient sqlMapClient;
	private static final Logger LOGGER = LoggerFactory
	        .getLogger(UserDAOImpl.class);

	public int deleteByPrimaryKey(String id) throws DaoException
	{
		User key = new User();
		key.setId(id);
		int rows = 0;
		try
		{
			rows = sqlMapClient.delete(
			        "tb_sys_user.ibatorgenerated_deleteByPrimaryKey", key);
		} catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rows;
	}

	public Integer insert(User record) throws DaoException
	{
		Integer insert = 0;
		try
		{
			insert = (Integer) sqlMapClient.insert(
			        "tb_sys_user.ibatorgenerated_insertSelective", record);
		} catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return insert;
	}

	public void insertSelective(User record) throws DaoException
	{
		try
		{
			sqlMapClient.insert("tb_sys_user.ibatorgenerated_insertSelective",
			        record);
		} catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public User selectByPrimaryKey(String id) throws DaoException
	{
		User key = new User();
		key.setId(id);
		User record = null;
		try
		{
			record = (User) sqlMapClient.queryForObject(
			        "tb_sys_user.ibatorgenerated_selectByPrimaryKey", key);
		} catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return record;
	}

	public int updateByPrimaryKeySelective(User record) throws DaoException
	{
		int rows = 0;
		try
		{
			rows = sqlMapClient.update(
			        "tb_sys_user.ibatorgenerated_updateByPrimaryKeySelective",
			        record);
		} catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rows;
	}

	public int updateByPrimaryKey(User record) throws DaoException
	{
		int rows = 0;
		try
		{
			rows = sqlMapClient.update(
			        "tb_sys_user.ibatorgenerated_updateByPrimaryKey", record);
		} catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rows;
	}

	public SqlMapClient getSqlMapClient()
	{
		return sqlMapClient;
	}

	public void setSqlMapClient(SqlMapClient sqlMapClient)
	{
		this.sqlMapClient = sqlMapClient;
	}

	public ResultDto<User> findUserInfoForPage(UserDTO userDTO)
	        throws DaoException
	{
		ResultDto<User> result = new ResultDto<User>();
		try
		{
			List<User> userList = sqlMapClient.queryForList(
			        "tb_sys_user.find_user_page", userDTO);
			result.setResult(userList != null ? userList
			        : new ArrayList<User>());
			BeanUtils.copyProperties(userDTO.getPagination(),
			        result.getPagination());
			result.getPagination().setTotal(
			        (Integer) sqlMapClient.queryForObject(
			                "tb_sys_user.find_user_page_count", userDTO));
		} catch (SQLException e)
		{
			LOGGER.error("findUserInfoForPage exception", e);
			throw new DaoException("findUserInfoForPage exception", e);
		}
		return result;
	}

	public List<User> queryUserList(User user) throws DaoException
	{
		List<User> record = null;
		try
		{
			record = sqlMapClient.queryForList(
			        "tb_sys_user.ibatorgenerated_select", user);
			record = (record == null ? new ArrayList<User>() : record);
		} catch (SQLException e)
		{
			LOGGER.error("queryUserList exception", e);
			throw new DaoException("queryUserList exception", e);
		}
		return record;
	}

}